﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NLog.Config;
using SmartCity.Passport.API.Applications.Commands;
using SmartCity.Passport.API.DTOS;
using SmartCity.Passport.ErrorCode;
using System.Reflection.Metadata;

namespace SmartCity.Passport.API.Controllers
{
    /// <summary>
    /// API控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ManagementController : ControllerBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<ManagementController> logger;
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="mediator">中介者</param>
        public ManagementController(ILogger<ManagementController> logger,IMediator mediator)
        {
            this.logger = logger;
            this.mediator = mediator;
        }
        /// <summary>
        /// 登录方法
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        [AllowAnonymous]
        public Task<APIResult<UserDTO>> LoginShow(LoginQueryCommand command)
        {
            try
            {
                logger.LogInformation("登陆成功");
                return mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("登录失败进入异常" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 测试方法
        /// </summary>
        /// <returns>返回任务</returns>
        [HttpGet]
        public IActionResult GetCss()
        {
            try
            {
                int num1 = 1;
                int num2 = 0;
                int name = num1 / num2;
                return Ok(name);
            }
            catch (Exception ex)
            {
                logger.LogError("测试方法失败进入异常"+ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 测试日志
        /// </summary>
        /// <returns>返回任务</returns>
        [HttpGet]
        public IActionResult GetLog()
        {
            int num = int.Parse("abc");
            return Ok(num);
        }
    }
}
